from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from home import models
from message.models import Message
from user.models import User as CustomUser
from django.db.models import Q

from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Message  # 假设有Room模型


class MessageGetView(APIView):
    def get_custom_user(self, request):
        phone = request.user.username
        custom_user, created = CustomUser.objects.get_or_create(
            phone=phone,
            defaults={
                "username": request.user.username,
                "status": 1,
                "role_id": 1,
            }
        )
        return custom_user

    def get(self, request, room_id):
        user = self.get_custom_user(request)
        messages = Message.objects.filter(room_id=room_id).order_by('timestamp')
        data = [
            {
                "id": msg.id,
                "sender": msg.sender.id,
                "sender_name": msg.sender.username,
                "sender_avatar": getattr(msg.sender, "avatar", ""),
                "content": msg.content,
                "timestamp": msg.timestamp,
                "is_read": msg.is_read
            }
            for msg in messages
        ]
        return Response({
            "code": 200,
            "msg": "获取成功",
            "data": data,
            "current_user_id": user.id,
            "room_id": room_id
        })


class MessagePostView(APIView):
    def get_custom_user(self, request):
        phone = request.user.username
        custom_user, created = CustomUser.objects.get_or_create(
            phone=phone,
            defaults={
                "username": request.user.username,
                "status": 1,
                "role_id": 1,
            }
        )
        return custom_user

    def post(self, request, user_id):
        # 允许前端传 sender 字段，否则用 token 里的用户
        sender_id = request.data.get("sender")
        if sender_id:
            try:
                sender = CustomUser.objects.get(id=sender_id)
            except CustomUser.DoesNotExist:
                return Response({"code": 404, "msg": "发送者不存在"}, status=404)
        else:
            sender = self.get_custom_user(request)

        content = request.data.get("content")
        room_id = request.data.get("room_id")
        if not content or not room_id:
            return Response({"code": 400, "msg": "消息内容和房间号不能为空"}, status=400)
        try:
            receiver = CustomUser.objects.get(id=user_id)
        except CustomUser.DoesNotExist:
            return Response({"code": 404, "msg": "接收者不存在"}, status=404)
        msg = Message.objects.create(sender=sender, receiver=receiver, content=content, room_id=room_id)
        return Response({"code": 200, "msg": "发送成功", "data": {
            "id": msg.id,
            "sender": msg.sender.id,
            "sender_name": msg.sender.username,
            "sender_avatar": getattr(msg.sender, "avatar", ""),
            "content": msg.content,
            "timestamp": msg.timestamp,
            "is_read": msg.is_read,
            "room_id": msg.room_id
        }})
